package com.luyu.rabbitmqdemo.mq.consumer;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.handler.annotation.Header;

import java.io.IOException;

/**
 * @author luYu
 */
@Configuration
public class ConsumerF {
    @RabbitListener(queues = "fanout.queue1")
    public void getMsgFanout1(String msg){
        System.out.println("发送的消息内容1:"+msg);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void getMsgFanout2(String msg){
        System.out.println("发送的消息内容2:"+msg);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "luyuQueue1"),
            exchange = @Exchange(name = "luyu.dir",type = ExchangeTypes.DIRECT),
            key = {"red","yellow"}
    ))
    public void getDir1(String msg){
        System.out.println("1发送的消息内容:"+msg);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "luyuQueue2"),
            exchange = @Exchange(name = "luyu.dir",type = ExchangeTypes.DIRECT),
            key = {"red"}
    ))
    public void getDir3(String msg){
        System.out.println("3发送的消息内容:"+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "luyuQueue3"),
            exchange = @Exchange(name = "luyu.dir",type = ExchangeTypes.DIRECT),
            key = {"call"}
    ))
    public void getDir4(String msg){
        System.out.println("4发送的消息内容:"+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "luyuQueue5"),
            exchange = @Exchange(name = "luyu.dir",type = ExchangeTypes.DIRECT),
            key = {"auto"}
    ))
    public void getDir5(String msg){
        System.out.println("5发送的消息内容:"+msg);
        System.out.println(1/0);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "error.queue"),
            exchange = @Exchange(name = "err.direct",type = ExchangeTypes.DIRECT),
            key = {"error"}
    ))
    public void getDir6(String msg){
        System.out.println("最后重试发送的消息:"+msg);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "dl.ttl.queue", durable = "true"),
            exchange = @Exchange(name = "dl.ttl.direct"),
            key = {"ttl"}
    ))
    public void listenDlQueue(String msg){
        System.out.println("接收到 dl.ttl.queue的延迟消息:"+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "test.queue", durable = "true"),
            exchange = @Exchange(name = "test.direct")
    ))
    public void listenOneQueue(String msg){
        System.out.println("test消息:"+msg);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "ack.queue", durable = "true"),
            exchange = @Exchange(name = "ack.exchange")
    ))
    public void listenAckQueue(String msg, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
        channel.basicAck(tag,true);
        System.out.println("消费1收到消息并手动确认消费消息:"+msg);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "return.queue", durable = "true"),
            exchange = @Exchange(name = "queue.not.exist")
    ))
    public void listenReturnQueue(String msg) throws IOException {
        System.out.println("消费2收到消息并手动确认消费消息:"+msg);
    }

}
